package view {
	
	import data.Tower;
	
	import events.GameEvent;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	import utils.Image;
	import utils.SwfElements;

	/**
	 * 建筑选择器 
	 * @author 1
	 * 
	 */	
	public class BuildBar extends Sprite{
		
		/**
		 * 容器 
		 */		
//		public var box:Sprite;
		/**
		 * 声明SWF 文件对象  
		 */		
		private var swfElement:SwfElements;
		
		/**
		 * 建筑物选择器的单历 
		 */		
		private static var instence:BuildBar;
		
		/**
		 * 生成source的get和set方法,传入被点击的地块以确定选择器的位置和要建设的塔的位置 
		 */		
//		private var _source:Image;
		
		/**
		 * 声名全局Image变量 
		 */		
		public static var landImg:Image=null;
		
		/**
		 * 用于设置建设物选择器的visiable属性; 
		 */		
		public var isShow:Boolean = false;
		
		/**
		 *  建筑物选择器外观;
		 */		
		private var buildBarMC:MovieClip;
		
		/**
		 * 箭塔 
		 */		
		private var tower1MC:MovieClip;
		
		/**
		 * 兵塔 
		 */	
		private var tower2MC:MovieClip;
		
		/**
		 * 魔法塔 
		 */	
		private var tower3MC:MovieClip;
		/**
		 * 炮塔 
		 */
		private var tower4MC:MovieClip;
		
		private var DockBtn:MovieClip;
		
		private var sp:Sprite=new Sprite();

		public function BuildBar(){
			swfElement = new SwfElements();
			addChild(swfElement);
			this.addEventListener(GameEvent.SWF_LOADER_COMPLETE,swfCompleteHandle);
		}
		
		/**
		 * 单历 
		 * @return 
		 * 
		 */		
		public static function getInstence():BuildBar{
			if(!instence) instence = new BuildBar();
			return instence;
		}
		
		/**
		 * 当swf加载完成时获取其中影片剪辑的值 
		 * @param event
		 * 
		 */		
		protected function swfCompleteHandle(event:GameEvent):void{
			buildBarMC = swfElement.getMC("BuildBarMC");
			addChild(buildBarMC);
			addTowers();
			
			addEventListener(MouseEvent.MOUSE_OVER,mouseOverHandle);
			addEventListener(MouseEvent.MOUSE_OUT,mouseOutHandle);
			addEventListener(MouseEvent.CLICK,clickHandle);
		}
		
		
		private function clickHandle(event:MouseEvent):void{
			var mc:MovieClip = event.target as MovieClip;
			//将当前build的位置,所选择的塔的类型,包装成一个对象发送出去
			var obj:Object={target:mc,px:x,py:y};
			var evt:GameEvent = new GameEvent(GameEvent.CLICK_BUILD_TYPE,obj);
			dispatchEvent(evt);
		}
		
		private function mouseOverHandle(event:MouseEvent):void{
			var mc:MovieClip = event.target as MovieClip;
			var tower:Tower = new Tower(null);
			var r:uint = tower.getTowerR(mc.name);
			var point:Point = new Point(x,y);
			var obj:Object={p:point,r:r}
			var evt:GameEvent = new GameEvent(GameEvent.ATTACK_RANGE,obj);
			this.dispatchEvent(evt);
		}
		
		private function mouseOutHandle(event:MouseEvent):void{
			var evt:GameEvent = new GameEvent(GameEvent.CLEAR_TOWERRANGE);
			this.dispatchEvent(evt);
		}
		
		/**
		 * 添加塔的选择按钮 
		 * 
		 */	
		private function addTowers():void{
			tower1MC = swfElement.getMC("Tower1MC");
			tower1MC.name = "Tower1";
			tower2MC = swfElement.getMC("Tower2MC");
			tower2MC.name = "Tower2";
			tower3MC = swfElement.getMC("Tower3MC");
			tower3MC.name = "Tower3";
			tower4MC = swfElement.getMC("Tower4MC");
			tower4MC.name = "Tower4";
			
//			DockBtn = swfElement.getMC("DockBtn");
//			DockBtn.name = "DockBtn";
			
			addChild(tower1MC);
			addChild(tower2MC);
			addChild(tower3MC);
			addChild(tower4MC);
			
			tower1MC.x = buildBarMC.x - 40;
			tower1MC.y = buildBarMC.y - 45;
			
			tower2MC.x = buildBarMC.x + 40;
			tower2MC.y = buildBarMC.y - 45;
			
			tower3MC.x = buildBarMC.x - 40;
			tower3MC.y = buildBarMC.y + 35;
			
			tower4MC.x = buildBarMC.x + 40;
			tower4MC.y = buildBarMC.y + 35;
			
			tower1MC.buttonMode = true;
			tower2MC.buttonMode = true;
			tower3MC.buttonMode = true;
			tower4MC.buttonMode = true;
			
		}		
		
			
		/**
		 * 建筑物选择器在显示和隐藏两种状态之间切换; 
		 * 
		 */		
		public function orShow():void{
			isShow = ! isShow;
			visible = isShow;
		}
		/**
		 * 隐藏; 
		 * 
		 */
		public function setHid():void{
			visible = false;
		}
		
	}
}